package com.vsd.service.impl;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.util.SqlUtils;
import com.vsd.domain.City;
import com.vsd.repository.CityRepository;
import com.vsd.service.CityService;
import com.zy.sys.proto.MOCommon.MCity;
import com.zy.sys.proto.MOCommon.MCityList;

@Service
@Transactional
public class CityServiceImpl implements CityService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(CityServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private CityRepository cityRepository;

	@Override
	public MCityList.Builder selectByLocation(Integer location,String parent) {
		MCityList.Builder ret = MCityList.newBuilder();
		if(location==1){
			Collection<City> list = cityRepository.findByLevelAndState(1, 1,new Sort(Direction.DESC, "sort"));
			for (City city : list) {
				MCity.Builder c = MCity.newBuilder();
				c.setCode(city.getCode());
				c.setHot(city.getIsHot());
				c.setName(city.getName());
				ret.addList(c);
			}
			
		}else if(location==2){
			Collection<City> list = cityRepository.findByCodeLikeAndLevelGreaterThanAndState(parent+"%",1, 1,new Sort(Direction.DESC, "sort"));
			
			for (int i=0;i<list.size() ;i++) {
				City city = list.iterator().next();
				if(city.getLevel()==2){
					MCity.Builder c = MCity.newBuilder();
					c.setCode(city.getCode());
					c.setHot(city.getIsHot());
					c.setName(city.getName());
					for (City city2 : list) {
						if(c.getCode().equals(city2.getParent())){
							MCity.Builder c2 = MCity.newBuilder();
							c2.setCode(city2.getCode());
							c2.setHot(city2.getIsHot());
							c2.setName(city2.getName());
							c.addSons(c2);
						}
					}
					ret.addList(c);
					list.remove(city);
				}
			}
		}else if(location==3){
			Collection<City> list = cityRepository.findByIsHotAndLevelAndState(1, 1, 1,new Sort(Direction.DESC, "sort"));
			for (City city : list) {
				MCity.Builder c = MCity.newBuilder();
				c.setCode(city.getCode());
				c.setHot(city.getIsHot());
				c.setName(city.getName());
				ret.addList(c);
			}
		}
		
		return ret;
	}

	@Override
	public City findByNameAndState(String name) {
		Collection<City> list = cityRepository.findByNameAndState(name,1);
		if(list.size()>0) return list.iterator().next();
		return null;
	}
	
	@Override
	public City findDefault() {
		Collection<City> list = cityRepository.findByLevelAndState(1,1,new Sort(Direction.DESC, "sort"));
		if(list.size()>0) return list.iterator().next();
		return null;
	}
	
	@Override
	public Collection<City> findByLevel(Integer level,String parentCode) {
		Collection<City> list = null;
		if(StringUtils.isNoneBlank(parentCode)){
			list = cityRepository.findByLevelAndParent(level,parentCode,new Sort(Direction.DESC, "sort"));
		}else{
			list = cityRepository.findByLevelAndState(level,1,new Sort(Direction.DESC, "sort"));
		}
		return list;
	}

	@Override
	public City findByCode(String code) {
		Collection<City> list = cityRepository.findByCode(code);
		if(!list.isEmpty()) return list.iterator().next();
		return null;
	}

	@Override
	public Page<City> findAll(String areaCode, Pageable p) {
		Page<City> list = null;
		if(StringUtils.isNotBlank(areaCode)){
			list = cityRepository.findByParent(areaCode, p);
		}else{
			list = cityRepository.findByLevel(1, p);
		}
		return list;
	}

	@Override
	public Collection<City> findByParent(String code, Sort sort) {
		// TODO Auto-generated method stub
		return cityRepository.findByParent(code, sort);
	}

	@Override
	public City findOne(String id) {
		// TODO Auto-generated method stub
		return cityRepository.findOne(id);
	}

	@Override
	public void save(City c) {
		// TODO Auto-generated method stub
		cityRepository.save(c);
	}

	@Override
	public int updateByPrimaryKeySelective(City c) {
		// TODO Auto-generated method stub
		try {
			String sql = SqlUtils.update(c, true, " where id = '"+c.getId()+"'");
			return jdbcTemplate.update(sql);
		}catch (Exception e) {
			return 0;
		}
	}


	
}
